<?php
include(dirname(dirname(dirname(__FILE__))) . '/bootstrap.php');

//引入抓取宝贝信息插件程序
include 'simplehtmldom_1_5/simple_html_dom.php';

/*
 * 抓取宝贝信息
 * 暂只支持 taobao tmaill
 */
class takeBaoBeiInfo
{

    public function __construct($url, $num)
    {
        $this->num = $num;
        $this->url = $url;
    }

    public function multiFileGetHtml()
    {
        $baobei_html = '';
        $i           = 0;

        while (trim_empty($baobei_html) && $i < $this->num) {

            $baobei_html = file_get_html($this->url);

            $i++;
        }
        return $baobei_html;
    }

    public function getDomainAndIdByBaobeiUrl()
    {
        $url_arr = getDomainAndIdByBaobeiUrl($this->url);

        if (is_array($url_arr) && !empty($url_arr)) {
            return $url_arr;
        }

        return false;
    }

    public function getStrFenCi($str){
        $data = array('str'=>$str);
        $ret = json_decode(curl_post_contents('http://www.whoopsfeed.com/love/api/fenci/str_fenci.php',$data));

        return isset($ret) ? $ret : '';
    }

    public function makeBaoBeiInfoArray()
    {
        $baobeiinfo = array();

        $domain = $this->getDomainAndIdByBaobeiUrl();

        if ($domain) {
            $baobei_html = $this->multiFileGetHtml();

            switch ($domain['domain']) {
                case 'taobao':
                    $shop_title_dom = 'a.hCard';
                    $price_dom      = 'strong[id=J_StrPrice]';
                    break;

                case 'tmall':
                    $shop_title_dom = 'h3.shop-title a';
                    $price_dom      = 'strong[class=J_originalPrice]';
                    break;
            }
            //抓取信息不为空
            if (!empty($baobei_html)) {

                @$get_shop_title = $baobei_html->find($shop_title_dom, 0)->plaintext;
                $shop_title = isset($get_shop_title) ? $get_shop_title : '';
                if ($shop_title) {
                    $baobeiinfo['shop_title'] = $shop_title;
                }

                @$get_title = $baobei_html->find("div.tb-detail-hd h3", 0)->plaintext;
                $title = isset($get_title) ? $get_title : '';
                if ($title) {
                    $baobeiinfo['title'] = $title;

                    $baobeiinfo['tags'] = utf8togbk($this->getStrFenCi(gbk2utf8($title)));
                }

                @$get_price = $baobei_html->find($price_dom, 0)->plaintext;
                $price = isset($get_price) ? $get_price : '';
                if ($price) {
                    $baobeiinfo['price'] = str_replace('&yen;','',$price);
                }

                $img_arr = array();
                // Extract images
                foreach ((array)$baobei_html->find('#J_UlThumb img') as $key => $element) {
                    $img_arr[] = $element->src;
                }
                if ($img_arr) {
                    $baobeiinfo['img_urls'] = !empty($img_arr) ? implode("|", $img_arr) : array();
                }
            }
        }
        return $baobeiinfo;
    }
}

/*
 * 获取宝贝信息
 */
$url = isset($_REQUEST['url']) ? $_REQUEST['url'] : '';

if ($url) {
    $takeBaoBeiInfo = new takeBaoBeiInfo($url, 2);

    $baobei_info = $takeBaoBeiInfo->makeBaoBeiInfoArray();

    if ($baobei_info && !empty($baobei_info['shop_title']) && !empty($baobei_info['title']) && !empty($baobei_info['price']) && !empty($baobei_info['img_urls'])) {

        echo json_encode(gbk2utf8($baobei_info));
    } else {
        echo json_encode(array());
    }

} else {
    echo json_encode(array());
}

exit;
?>